////////////////////////////////////////////////////////////////////////////////
// Includes
////////////////////////////////////////////////////////////////////////////////

var http = require("http");
var inspect = require("util").inspect;
var MariaSQLClient = require("mariasql");

var express = require('express');             // imports the framework into your app.
var path = require('path');                   // core Node module for working with and handling paths.
var favicon = require('serve-favicon');       // Express middleware for serving a favicon
var logger = require('morgan');               // Express middleware for logging requests and responses.
var cookieParser = require('cookie-parser');  // Express middleware that helps you with handling cookie
var bodyParser = require('body-parser');      // Express middleware you probably want to use if you're doing anything with forms
var session = require('express-session');     // Express middleware for session management 

////////////////////////////////////////////////////////////////////////////////
// Setup routes
////////////////////////////////////////////////////////////////////////////////

var routes = require('./routes/index'); // dummy pages to show you how routing works
var users = require('./routes/users');  // dummy pages to show you how routing works
var auth = require('./routes/auth');
var api = require('./routes/api');
var api_v1 = require('./routes/api/v1');
var route_error = require('./routes/error');  // route to error pages


////////////////////////////////////////////////////////////////////////////////
// Express setup
////////////////////////////////////////////////////////////////////////////////

var app = express();

if (app.get('env') === 'development') {
    app.locals.pretty = true; // Set this to true to beautify html output
}


// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');


// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));


// setup static virtual directory for js, css and img
app.use('/js', express.static(path.join(__dirname, 'public', 'scripts')));
app.use('/css', express.static(path.join(__dirname, 'public', 'stylesheets')));
app.use('/img', express.static(path.join(__dirname, 'public', 'images')));

// setup session
// ZX: We need to setup session before we excute any code for the route
app.use(session({
  key: 'project_radar_session',
  store: session.MemoryStore(),
  secret: 'project_radar_9453',
  cookie: { maxAge: 60000 },
  proxy: undefined,
  resave: false,
  rolling: false,
  saveUninitialized: true
}))


// setup routing to their respective route handlers
app.use('/', routes);
app.use('/users', users);
app.use('/auth', auth);
app.use('/error', route_error);
app.use('/api', api);


// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});


// When you want to use some variables or functions from another file, you attach them to the module.exports.
module.exports = app;
